import 'package:HealthU/Service/ApiService.dart';
import 'package:HealthU/model/ResponseModel.dart';
import 'package:HealthU/model/UserModel.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';


class LoginViewModel extends ChangeNotifier {
  bool _isLoading = false;
  bool _isLoggedIn = false;
  String _token = "";
  int _userId = 0;

  bool get isLoading => _isLoading;
  bool get isLoggedIn => _isLoggedIn;
  String get token => _token;
  int get userId => _userId;




  LoginViewModel(){



    getLoginStatus();





  }

  void getLoginStatus() async {

    var value =  await loadData('_isLoggedIn');

    var userId =  await loadData('_userId');

    var token =  await loadData('_token');

    if (value is bool) {

      _isLoggedIn = value as bool;

    }

    if (userId is int) {

      _userId = userId as int;


    }

    if(token is String){
      _token = token as String;
    }

      notifyListeners();
  }

  Future<void> login(String username, String password) async {
    // 模拟网络请求
    _isLoading = true;
    notifyListeners();

    final apiService = ApiService();

    _isLoading = true;
    final response = await apiService.login(username, password);
    _isLoading = false;


    try {
      // 可能会抛出异常的代码

      final ResponseModel model = ResponseModel.fromJson(response);

      int code = model.code;

      if(code == 200){

        String token = model.data["token"];

        int userId = model.data["id"];

        saveData("_isLoggedIn", true);

        saveData("_userId", userId);

        saveData("_token", token);


        _token = token;

        _userId = userId;

      _isLoggedIn = true;
      }

    } catch (e) {
      // 处理异常的代码
    }

    // 假设这里是根据网络请求结果判断登录成功与否


    notifyListeners();
  }

  Future<UserModel?> getUserInfo() async {

    final apiService = ApiService();

    final response = await apiService.getUserInfo(_userId);


    try {
      // 可能会抛出异常的代码

      final ResponseModel model = ResponseModel.fromJson(response);

      int code = model.code;

      if(code == 200) {

        UserModel user = UserModel.fromJson(model.data);


        return user;


      }
    } catch (e) {


    }
    return null;
  // 处理异常的代码



  }

    void logout(){
    saveData("_isLoggedIn", false);
    _isLoggedIn = false;
    _token = "";

    notifyListeners();

  }

  // 存储数据
  Future<void> saveData(String key,dynamic value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    if (value is String) {
      // 处理字符串类型的值
      prefs.setString(key, value);
    } else if (value is int) {
      // 处理整数类型的值

      prefs.setInt(key, value);
    } else if (value is bool) {
      // 处理布尔类型的值

      prefs.setBool(key, value);
    } else {
      // 其他处理逻辑
    }

  }

// 读取数据
  Future<dynamic> loadData(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();


    dynamic value = prefs.get(key);


    return value ?? '';
  }
}

final loginViewModelProvider = ChangeNotifierProvider((ref) => LoginViewModel());

